﻿// Software timer.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include "./software_timer/software_timer.h"
#include "./software_timer/software_timer_c.h"
#include <sys/timeb.h>
#include <windows.h>
using namespace std;

unsigned int timer_get_tick(void)
{
	unsigned int time = 0;

	struct timeb rawtime;
	ftime(&rawtime);
	time = (unsigned int)rawtime.time * 1000 + rawtime.millitm;

	return time;
}

DWORD WINAPI work(LPVOID lParam)
{
	while (1)
	{
		software_timer_check();
	}
}

void timer_init(void)
{
	set_timer_tick_cb(timer_get_tick);
	HANDLE hTread = CreateThread(NULL, 0, work, 0, 0, NULL);
}

void timer_cb(void* data)
{
	std::cout << (char*)data << std::endl;
}

void* s_timer_h = NULL;

void timer_del_test(void* data)
{
	std::cout << (char*)data << std::endl;
	software_timer_remove(s_timer_h);
}

void timer_auto_del_test(void)
{
	SoftTimer timer1(1000, timer_cb, (void*)"timer_auto_del_test -> 1s\r\n");
	SoftTimer timer2(1000, TIMER_START_SINGLE_DEL, timer_cb, (void*)"timer_auto_del_test(no del) -> 1s\r\n");
	SoftTimer timer3(1000, TIMER_START_SINGLE, timer_cb, (void*)"timer_auto_del_test(no del, memory leak) -> 1s\r\n");
}

void c_test(void)
{
	void* timer_h = NULL;
	//software_timer_create_ex(1000, TIMER_START_SINGLE_DEL, timer_cb, (void*)"c timer single del test -> 1s\r\n", &timer_h);
#ifndef AUTO_PARM_CHECK
	/* 测试单次定时器 */
	software_timer_create(1000, TIMER_START_SINGLE, timer_cb, (void*)"timer single test -> 1s\r\n");
	software_timer_create(2000, TIMER_START_SINGLE, timer_cb, (void*)"timer single test -> 2s\r\n");
	software_timer_create(3000, TIMER_START_SINGLE, timer_cb, (void*)"timer single test -> 3s\r\n");

	///* 测试定时器连续运行*/
	//software_timer_create(500, TIMER_START_CYCLE, timer_cb, (void*)"timer cycle test\r\n");

	///* 测试删除定时器 */
	//software_timer_create_ex(200, TIMER_START_CYCLE, timer_cb, (void*)"timer cycle wait delete test\r\n", &timer_h);
	//software_timer_create_ex(2201, TIMER_START_SINGLE, timer_del_test, (`void*)"timer cycle wait delete test\r\n", &s_timer_h);
#else
	/* 测试单次定时器 */
	TIMER_CREATE(1000, TIMER_START | TIMER_SINGLE, timer, (void*)"timer single test -> 1s\r\n");
	TIMER_CREATE(2000, TIMER_START | TIMER_SINGLE, timer, (void*)"timer single test -> 2s\r\n");
	TIMER_CREATE(3000, TIMER_START | TIMER_SINGLE, timer, (void*)"timer single test -> 3s\r\n");

	/* 测试定时器连续运行*/
	TIMER_CREATE(5000, TIMER_START | TIMER_CYCLE, timer, (void*)"timer cycle test\r\n");

	/* 测试删除定时器 */
	TIMER_CREATE(200, TIMER_START | TIMER_CYCLE, timer, (void*)"timer cycle wait delete test\r\n", &timer_temp);
	TIMER_CREATE(2201, TIMER_START | TIMER_SINGLE, timer_del_test, (void*)& timer_temp);
#endif
}

void cb_cnt(void* data)
{
	cout << (*(int*)data)++ << endl;
}

int main()
{
	cout << "timer test" << endl;
	timer_init();
#if 1   // c++ test
	/* 测试对象被释放时自动删除已经停止的定时器 */
	timer_auto_del_test();
	/*  */
	int  i = 0;
	SoftTimer timer1(1000, cb_cnt, (void*)&i);
	timer1.start(TIMER_START_CYCLE);
	SoftTimer timer2;
	timer2.set_cb(timer_cb, (void*)"timer set cb -> 1s\r\n");
	timer2.start(1000, TIMER_START_SINGLE_DEL);
#else
	c_test();
#endif
	while (1);
}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件，或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来，若要再次打开此项目，请转到“文件”>“打开”>“项目”并选择 .sln 文件
